home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / MSG Demo 1.4 / source / Demo ƒ / file management.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  6.0 KB  |  240 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        file management.c
  4.  
  5. Purpose:    This module handles files for deBinHexing: opening the
  6.             input file, creating/opening the temp file, reading and
  7.             writing files, and finalizing them when we're done.
  8.  
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program in a file named "GNU General Public License".
  21. If not, write to the Free Software Foundation, 675 Mass Ave,
  22. Cambridge, MA 02139, USA.
  23.  
  24. \**********************************************************************/
  25.  
  26. #include "Script.h"
  27. #include "file management.h"
  28. #include "environment.h"
  29. #include "util.h"
  30. #include "program globals.h"
  31.  
  32. unsigned long    theFileType, theFileCreator;
  33. int                theFileFlags;
  34. unsigned long    theCreationDate, theModificationDate;
  35. FSSpec            inputFS, outputFS, tempFS;
  36. Boolean            deleteTheThing;
  37. int                inputRefNum, outputDFRefNum, outputRFRefNum;
  38. unsigned long    outputDFeof, outputRFeof;
  39.  
  40. void InitFiles(void)
  41. {
  42.     inputRefNum=outputDFRefNum=outputRFRefNum=0;
  43.     outputDFeof=outputRFeof=0L;
  44.     deleteTheThing=TRUE;
  45. }
  46.  
  47. int OpenInputFile(void)
  48. {
  49.     OSErr            isHuman;
  50.     HParamBlockRec    paramBlock;
  51.     
  52.     if (gHasFSSpecs)
  53.         isHuman=FSpOpenDF(&inputFS, fsRdPerm, &inputRefNum);
  54.     else
  55.         isHuman=HOpen(inputFS.vRefNum, inputFS.parID, inputFS.name,
  56.                         fsRdPerm, &inputRefNum);
  57.     if (!isHuman)
  58.     {
  59. //        GetEOF(inputRefNum, &blob->sourceDFeof);
  60.         SetFPos(inputRefNum, 1, 0L);
  61.         
  62.         paramBlock.fileParam.ioCompletion=0L;
  63.         paramBlock.fileParam.ioNamePtr=inputFS.name;
  64.         paramBlock.fileParam.ioVRefNum=inputFS.vRefNum;
  65.         paramBlock.fileParam.ioFDirIndex=0;
  66.         paramBlock.fileParam.ioDirID=inputFS.parID;
  67.         PBHGetFInfo(¶mBlock, FALSE);
  68.         theCreationDate=paramBlock.fileParam.ioFlCrDat;
  69.         theModificationDate=paramBlock.fileParam.ioFlMdDat;
  70.     }
  71.     else return kCantOpenInputFile;
  72. }
  73.  
  74. int CreateTempFile(void)
  75. {
  76.     int                    i;
  77.     Str255                timeStr;
  78.     OSErr                isHuman;
  79.     int                    resultCode;
  80.     
  81.     tempFS=outputFS;
  82.     tempFS.name[0]=0x05;
  83.     tempFS.name[1]='D';
  84.     tempFS.name[2]='e';
  85.     tempFS.name[3]='m';
  86.     tempFS.name[4]='o';
  87.     tempFS.name[5]='.';
  88.     NumToString(Time&0x7fffffff, timeStr);
  89.     for (i=1; i<=timeStr[0]; i++)
  90.         tempFS.name[++(tempFS.name[0])]=timeStr[i];
  91.     
  92.     if (gHasFSSpecs)
  93.         isHuman=FSpCreate(&tempFS, CREATOR, 'TeMp', smSystemScript);
  94.     else
  95.         isHuman=HCreate(tempFS.vRefNum, tempFS.parID, tempFS.name, CREATOR, 'TeMp');
  96.     
  97.     return (isHuman==noErr) ? allsWell : kCantCreateTempFile;
  98. }
  99.  
  100. int SetupTempFile(void)
  101. {
  102.     OSErr                isHuman;
  103.     
  104.     if (outputDFeof!=0)
  105.     {
  106.         if (gHasFSSpecs)
  107.             isHuman=FSpOpenDF(&tempFS, fsRdWrPerm, &outputDFRefNum);
  108.         else
  109.             isHuman=HOpenDF(tempFS.vRefNum, tempFS.parID,
  110.                             tempFS.name, fsRdWrPerm, &outputDFRefNum);
  111.     }
  112.     
  113.     if (isHuman!=noErr)
  114.         return kDiskWriteErr;
  115.     
  116.     if (outputRFeof!=0)
  117.     {
  118.         if (gHasFSSpecs)
  119.             isHuman=FSpOpenRF(&tempFS, fsRdWrPerm, &outputRFRefNum);
  120.         else
  121.             isHuman=HOpenRF(tempFS.vRefNum, tempFS.parID,
  122.                             tempFS.name, fsRdWrPerm, &outputRFRefNum);
  123.     }
  124.     
  125.     if (isHuman!=noErr)
  126.         return kDiskWriteErr;
  127.     
  128.     if (outputDFeof!=0)
  129.         isHuman=SetEOF(outputDFRefNum, outputDFeof);
  130.     if ((!isHuman) && (outputRFeof!=0))
  131.         isHuman=SetEOF(outputRFRefNum, outputRFeof);
  132.  
  133.     if (!isHuman)
  134.     {
  135.         FlushVol(0L, tempFS.vRefNum);
  136.         if (outputDFeof!=0)
  137.             SetFPos(outputDFRefNum, 1, 0L);
  138.         if (outputRFeof!=0)
  139.             SetFPos(outputRFRefNum, 1, 0L);
  140.     }
  141.     else
  142.     {
  143. // not enough room on disk to create temp file.  Check if we will eventually be
  144. // deleting a destination file of the same name; if so, ask user if they want to
  145. // delete that now.
  146.     }
  147.     
  148.     return (isHuman ? kDiskWriteErr : allsWell);
  149. }
  150.  
  151. void FinalizeFiles(Boolean good)
  152. {
  153.     HParamBlockRec        paramBlock;
  154.     FInfo                theInfo;
  155.     
  156.     if (inputRefNum)
  157.         FSClose(inputRefNum);
  158.     
  159.     if (outputDFRefNum)
  160.         FSClose(outputDFRefNum);
  161.     if (outputRFRefNum)
  162.             FSClose(outputRFRefNum);
  163.     
  164.     FlushVol(0L, tempFS.vRefNum);
  165.     
  166.     if (!good)
  167.     {
  168.         if(gHasFSSpecs)
  169.             FSpDelete(&tempFS);
  170.         else
  171.             HDelete(tempFS.vRefNum, tempFS.parID, tempFS.name);
  172.         
  173.         return;
  174.     }
  175.     
  176.     if (deleteTheThing)
  177.     {
  178.         if (gHasFSSpecs)
  179.             FSpDelete(&outputFS);
  180.         else
  181.             HDelete(outputFS.vRefNum, outputFS.parID, outputFS.name);
  182.     }
  183.     FlushVol(0L, tempFS.vRefNum);
  184.     
  185.     
  186.     if (gHasFSSpecs)
  187.         FSpGetFInfo(&tempFS, &theInfo);
  188.     else
  189.         HGetFInfo(tempFS.vRefNum, tempFS.parID, tempFS.name,
  190.                     &theInfo);
  191.  
  192.     Mymemset(&theInfo, 0, sizeof(theInfo));
  193.  
  194.     theInfo.fdType=theFileType;
  195.     theInfo.fdCreator=theFileCreator;
  196.     theInfo.fdFlags=theFileFlags;
  197.     theInfo.fdFlags&=~0x0100;    /* clear Inited bit */
  198.     
  199.     if (gHasFSSpecs)
  200.         FSpSetFInfo(&tempFS, &theInfo);
  201.     else
  202.         HSetFInfo(tempFS.vRefNum, tempFS.parID, tempFS.name,
  203.                     &theInfo);
  204.     FlushVol(0L, tempFS.vRefNum);
  205.     
  206.     paramBlock.fileParam.ioCompletion=0L;
  207.     paramBlock.fileParam.ioNamePtr=tempFS.name;
  208.     paramBlock.fileParam.ioVRefNum=tempFS.vRefNum;
  209.     paramBlock.fileParam.ioFDirIndex=0;
  210.     paramBlock.fileParam.ioDirID=tempFS.parID;
  211.     PBHGetFInfo(¶mBlock, FALSE);
  212.     paramBlock.fileParam.ioFlCrDat=theCreationDate;
  213.     paramBlock.fileParam.ioFlMdDat=theModificationDate;
  214.     PBHSetFInfo(¶mBlock, FALSE);    
  215.     FlushVol(0L, tempFS.vRefNum);    
  216.     
  217.     if (gHasFSSpecs)
  218.         FSpRename(&tempFS, outputFS.name);
  219.     else
  220.         HRename(tempFS.vRefNum, tempFS.parID,
  221.                 tempFS.name, outputFS.name);
  222.     
  223.     FlushVol(0L, tempFS.vRefNum);
  224. }
  225.  
  226. unsigned long ReadInputFile(int thisFile, unsigned char* buffer, unsigned long count)
  227. {
  228.     OSErr            isHuman;
  229.     unsigned long    temp;
  230.     
  231.     temp=count;
  232.     isHuman=FSRead(thisFile, &temp, buffer);
  233.     return ((isHuman==kEofErr) || (isHuman==noErr)) ? temp : kDiskReadErr;
  234. }
  235.  
  236. int WriteTempFile(int thatFile, unsigned char* buffer, unsigned long theLength)
  237. {
  238.     return (FSWrite(thatFile, &theLength, buffer)==noErr ? allsWell : kDiskWriteErr);
  239. }
  240.